package dao;

import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.*;
import util.EntityManagerUtil;

public class TouristDao {
	public static Tourist getTourist(Tourist tourist) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Tourist touristFind = new Tourist();
		touristFind = em.find(Tourist.class, tourist.getEmail());
		if (touristFind != null) {
			em.close();
			return touristFind;
		} else
			return null;
	}
	
	public static List<Tourist> getTourists() {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Query query = em.createQuery("SELECT c FROM Tourist c ");
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		List<Tourist> rnames = query.getResultList();
		em.close();
		return rnames;
	}
	

	public static Tourist getTouristNoCache(Tourist tourist) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Tourist c where c.email=:email";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Tourist touristFind = (Tourist) query.setParameter("email", tourist.getEmail()).getSingleResult();
		em.close();
		if (touristFind != null)
			return touristFind;
		else
			return null;
	}
	
	public static Tourist getTouristNoCache(String email) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Tourist c where c.email=:email";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Tourist touristFind;
		try {
			touristFind = (Tourist) query.setParameter("email", email).getSingleResult();
			em.close();
			return touristFind;
		} catch (Exception e) {
			em.close();
			// TODO Auto-generated catch block
		//	e.printStackTrace();
			return null;
		}
	}

	// ���Tourist success
	public static boolean addTourist(Tourist tourist) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Tourist touristFind = getTouristNoCache(tourist);
		} catch (Exception e) {
			em.getTransaction().begin();
			em.persist(tourist);
			em.getTransaction().commit();
			em.close();
			return true;
		}
		return false;
	}

	public static boolean delTourist(Tourist tourist) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Tourist touristFind = getTouristNoCache(tourist);
			if (touristFind != null) {
				Tourist delTourist = em.find(Tourist.class, tourist.getEmail());
				em.getTransaction().begin();
				em.remove(delTourist);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}
	
	public static Tourist getTouristByEmail(String email) {
			EntityManager em = EntityManagerUtil.getEntityManager();
			Query query = em.createQuery("SELECT c FROM Tourist c where c.email = :email ");
			query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
			Tourist tourist  = (Tourist) query.setParameter("email", email ).getSingleResult();
			em.close();
			System.out.println(tourist.getEmail());
			return tourist;
	}
	public static boolean updateTourist(Tourist tourist) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Tourist touristFind = getTouristNoCache(tourist);
			if (touristFind != null) {
				Tourist upTourist = em.find(Tourist.class, tourist.getEmail());
				em.getTransaction().begin();
				upTourist.setAge(tourist.getAge());
				upTourist.setName(tourist.getName());
				upTourist.setTelphone(tourist.getTelphone());
				em.persist(upTourist);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	public static void main(String args[]) throws Exception {
		Tourist tourist = new Tourist();
		tourist.setEmail("1");
		// System.out.println(TouristDao.getTourist(tourist).getTelphone());
		// System.out.println("11"+TouristDao.getTouristNoCache(tourist).getTelphone());
		Tourist tourist3 = new Tourist();
		tourist3.setEmail("4");
		tourist3.setAge(1111);
		tourist3.setName("12221111");
		//tourist3.setTelphone(11111);
//		if (TouristDao.addTourist(tourist3)) {
//			System.out.println("111");
//		} else {
//			System.out.println("222");
//		}
		if (TouristDao.updateTourist(tourist3)) {
			System.out.println("111");
		} else {
			System.out.println("222");
		}
	}
}
